/*--------------------------------------------------------------------------
Correlation of absolute vs relative indicators
Last modification: 04/30/2021
Uses:	           merged_data.dta
Produces:          Table with correlation btw abs/rel indicators and fairness
--------------------------------------------------------------------------*/

global raw  "C:\Users\USERNAME\"
global code "${root}\code"
global data "${root}\data"
global res  "${root}\results"


#delimit; 

use "${data}\merged_data.dta", replace;

* Correlation between indicators and perceptions;
tempname temporal; tempfile temporal_file; postfile `temporal' str40(Country Perception Objective Correlation P_Value Standard_Error_Reg Standard_Error_Boot Interpolation) using `temporal_file', replace;	

local fairness "very_unfair unfair fair very_fair unfair_all distribucion_justa";
local inequality "gini_lel atk_05_original atk_1_original cv_original gen_entrophy_original gini_abs_original gini_lel_original gini_nozero_original mean_logdev_original ratio75_25_original ratio90_10_original theil_original variance_original kolm_original";

levelsof pais, local(countries); local countries "`countries' pool lac";
foreach country of local countries {;

	use "${data}\merged_data.dta", replace;

	if ("`country'" == "lac") collapse (mean) `fairness' `inequality', by(ano);
	if ("`country'" != "lac" & "`country'" != "pool" ) keep if pais == "`country'";
	
	foreach fair of local fairness {; 
		foreach ineq of local inequality {;
	
			local ineq_lab   : variable label `ineq';
			local unfair_lab : variable label `fair';
						
			qui sum `fair', d ; gen `fair'_std = (`fair'-r(mean))/r(sd);
			qui sum `ineq', d ; gen `ineq'_std = (`ineq'-r(mean))/r(sd);
			
			reg `fair'_std `ineq'_std, robust;
			loc pvalue = (2 * ttail(e(df_r), abs(_b[`ineq']/_se[`ineq'])));
			loc se_reg = _se[`ineq'];
				
			corr `fair' `ineq'; loc corr = r(rho);
			
			bootstrap r(rho), reps(500) seed(1): correlate `fair' `ineq';
			mat aux = r(table);	loc se_boot = aux[2,1];
			post `temporal' ("`country'") ("`unfair_lab'") ("`ineq_lab'") ("`corr'") ("`pvalue'") ("`se_reg'") ("`se_boot'") ("Not Interpolating");

			matrix drop aux; drop *_std;
			
		};
	};
};

postclose `temporal'; use `temporal_file', clear; destring, replace; compress;

replace Objective = subinstr(Objective,"(mean) ","",.);
replace Perception = "% Only Unfair" if Perception == "(mean) unfair";
replace Perception = "% Very Unfair" if Perception == "(mean) very_unfair";
replace Perception = "% Unfair or Very Unfair" if Perception == "(mean) unfair_all";
drop if inlist(Perception,"(mean) distribucion_justa","(mean) fair","(mean) very_fair", "Mean index", "% Fair", "% Very Fair");

foreach syntax in " value" "_original" {;
	replace Objective = "Atkinson, A(0.5)"                  if Objective == "atk_05`syntax'"      ;
	replace Objective = "Atkinson, A(1)"                    if Objective == "atk_1`syntax'"       ;
	replace Objective = "Atkinson, A(2)"                    if Objective == "atk_2`syntax'"       ;
	replace Objective = "Coefficient Variation"             if Objective == "cv`syntax'"          ;
	replace Objective = "Generalized entropy, GE(2)"        if Objective == "gen_entrophy`syntax'";
	replace Objective = "Absolute Gini"                     if Objective == "gini_abs`syntax'"    ;
	replace Objective = "Gini coefficient"                  if Objective == "gini_lel`syntax'"    ;
	replace Objective = "Gini coefficient (no zero income)" if Objective == "gini_nozero`syntax'" ;
	replace Objective = "Mean log deviation, GE(0)"         if Objective == "mean_logdev`syntax'" ;
	replace Objective = "Ratio 75/25"                       if Objective == "ratio75_25`syntax'"  ;
	replace Objective = "Ratio 90/10"                       if Objective == "ratio90_10`syntax'"  ;
	replace Objective = "Theil index, GE(1)"                if Objective == "theil`syntax'"       ;
	replace Objective = "Variance"                          if Objective == "variance`syntax'"    ;
	replace Objective = "Kolm, K(1)"                        if Objective == "kolm`syntax'"        ;
};

export excel using "${res}\Data Analysis.xlsx", sheet("Relative vs. Absolute") sheetreplace firstrow(variables);


/* End of do-file */
exit
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
